﻿Imports DevExpress.XtraEditors

Public Class frmLoaiChietTinh
    Dim Me_ThemSua As String
    Dim Me_Dtb, Me_Dtb_MaLoaiChietTinh As New DataTable

    Private Sub frmLoaiChietTinh_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmLoaiChietTinh_Exists = False
    End Sub

    Private Sub frmLoaiChietTinh_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitLookup()
        InitGrid()
        Me.lueLoaiChietTinh.ItemIndex = 0
        LoadGrid()
        Status(0)
    End Sub

    Private Sub InitLookup()

        Me.lueLoaiChietTinh.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("maloaichiettinh", "Mã loại", 20, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("tenloaichiettinh", "Tên loại", 80, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("loaichiettinh", "Loại chiết tính", 85, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Center)})
        Me.lueLoaiChietTinh.Properties.DisplayMember = "loaichiettinh"
        Me.lueLoaiChietTinh.Properties.ValueMember = "maloaichiettinh"

        Dim myLoaiChietTinhF As New LoaiChietTinhF

        Me_Dtb_MaLoaiChietTinh = myLoaiChietTinhF.GetAll
        Me.lueLoaiChietTinh.Properties.DataSource = Me_Dtb_MaLoaiChietTinh

    End Sub

    Private Sub LoadLookup()

        Dim myLoaiChietTinhF As New LoaiChietTinhF

        Me_Dtb_MaLoaiChietTinh = myLoaiChietTinhF.GetAll
        Me.lueLoaiChietTinh.Properties.DataSource = Me_Dtb_MaLoaiChietTinh

    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1

            i = 0
            .Columns(i).FieldName = "ma"
            .Columns(i).Visible = False

            i = i + 1
            .Columns(i).FieldName = "ten"
            .Columns(i).Caption = "Tên vật tư"
            .Columns(i).Width = 200
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "dvt"
            .Columns(i).Caption = "Đvt"
            .Columns(i).Width = 40
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "sldl"
            .Columns(i).Caption = "Điện lực"
            .Columns(i).Width = 50
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            i = i + 1
            .Columns(i).FieldName = "slkh"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 50
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            i = i + 1
            .Columns(i).FieldName = "tenhangmuc"
            .Columns(i).Caption = "Hạng mục"
            .Columns(i).Group()
            .ExpandAllGroups()

        End With

    End Sub

    Private Sub LoadGrid()
        Dim myLoaiChietTinhCtF As New LoaiChietTinhCtF
        Me_Dtb = myLoaiChietTinhCtF.GetWhereMaLoaiChietTinh(Me.lueLoaiChietTinh.EditValue)
        Me.GridControl1.DataSource = Me_Dtb
        Me.GridView1.ExpandAllGroups()
       
    End Sub

    Private Sub lueLoaiChietTinh_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lueLoaiChietTinh.EditValueChanged
        LoadGrid()
    End Sub

    Private Sub AddDetail()
        'dua danh sach ma trong grid vao _dtb
        frmLoaiChietTinh_Dtb = New System.Data.DataTable("MyDTB")
        frmLoaiChietTinh_Dtb.Columns.Add("ma", GetType(String))

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            frmLoaiChietTinh_Dtb.Rows.Add(New Object() {Me.GridView1.GetDataRow(i).Item("ma")})
        Next

        'mo form tra cuu
        Project_Form = Me.Name
        Dim f As New frmDanhMuc_TraCuu
        f.ShowDialog()

        'them vat tu
        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable
        For i = 0 To frmDanhMuc_TraCuu_Dtb.Rows.Count - 1
            myDtb = myDanhMucF.GetWhereMa(frmDanhMuc_TraCuu_Dtb.Rows(i).Item("ma"))
            If myDtb.Rows.Count = 0 Then Exit Sub

            Me_Dtb.Rows.Add()
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ma") = myDtb.Rows(0).Item("ma")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ten") = myDtb.Rows(0).Item("ten")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dvt") = myDtb.Rows(0).Item("dvt")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("sldl") = 0
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("slkh") = 0
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("tenhangmuc") = myDtb.Rows(0).Item("tenhangmuc")
        Next

    End Sub

    Private Sub Status(ByVal i As Integer)
        Select Case i
            Case 0 'xem
                Me.lueLoaiChietTinh.Visible = True
                Me.txtMaLoaiChietTinh.Visible = False
                Me.txtTenLoaiChietTinh.Visible = False

                Me.cmdThemLoai.Text = "T&hêm loại"
                Me.cmdSuaLoai.Text = "&Sửa"

                Me.cmdXoaLoai.Enabled = True

                Me.cmdThemVatTu.Enabled = False
                Me.cmdXoaVatTu.Enabled = False

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = False
                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = False

            Case 1 'them
                Me.lueLoaiChietTinh.Visible = False
                Me.txtMaLoaiChietTinh.Visible = True
                Me.txtTenLoaiChietTinh.Visible = True

                Me.txtMaLoaiChietTinh.Enabled = True
                Me.txtTenLoaiChietTinh.Enabled = True

                Me.cmdThemLoai.Text = "&Ghi"
                Me.cmdSuaLoai.Text = "&Bỏ qua"

                Me.cmdXoaLoai.Enabled = False

                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = True
                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = True

            Case 2 'sua
                Me.lueLoaiChietTinh.Visible = False
                Me.txtMaLoaiChietTinh.Visible = True
                Me.txtTenLoaiChietTinh.Visible = True

                Me.txtMaLoaiChietTinh.Enabled = False
                Me.txtTenLoaiChietTinh.Enabled = True

                Me.cmdThemLoai.Text = "&Ghi"
                Me.cmdSuaLoai.Text = "&Bỏ qua"

                Me.cmdXoaLoai.Enabled = False

                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = True
                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = True

        End Select
    End Sub

    Private Function CheckKey() As Boolean
        Dim myLoaiChietTinhF As New LoaiChietTinhF
        If myLoaiChietTinhF.ExistsMaLoaiChietTinh(Me.txtMaLoaiChietTinh.Text.Trim) Then
            XtraMessageBox.Show("Mã loại chiết tính đã tồn tại", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtMaLoaiChietTinh.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Function CheckInput() As Boolean
        'maloaichiettinh
        If Me.txtMaLoaiChietTinh.EditValue.ToString.Length <> 2 Then
            XtraMessageBox.Show("Mã loại chiết tính không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtMaLoaiChietTinh.Focus()
            Return False
            Exit Function
        End If

        'tenloaichiettinh
        If Me.txtTenLoaiChietTinh.Text.Trim = "" Then
            XtraMessageBox.Show("Tên loại chiết tính không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtTenLoaiChietTinh.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function SaveMaster() As Boolean
        Dim myLoaiChietTinh As New LoaiChietTinh
        Dim myLoaiChietTinhF As New LoaiChietTinhF

        myLoaiChietTinh.MaLoaiChietTinh = Me.txtMaLoaiChietTinh.Text.Trim
        myLoaiChietTinh.TenLoaiChietTinh = Me.txtTenLoaiChietTinh.Text.Trim

        If Not myLoaiChietTinhF.Save(myLoaiChietTinh) Then
            XtraMessageBox.Show("Ghi tiêu đề loại chiết tính không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function SaveDetail() As Boolean
        Dim myLoaiChietTinhCt As New LoaiChietTinhCt
        Dim myLoaiChietTinhCtF As New LoaiChietTinhCtF

        If Not myLoaiChietTinhCtF.Delete(Me.txtMaLoaiChietTinh.Text.Trim) Then
            Exit Function
            Return False
        End If

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            myLoaiChietTinhCt.MaLoaiChietTinh = Me.txtMaLoaiChietTinh.Text.Trim
            myLoaiChietTinhCt.Ma = Me.GridView1.GetDataRow(i).Item("ma")
            myLoaiChietTinhCt.Sldl = Me.GridView1.GetDataRow(i).Item("sldl")
            myLoaiChietTinhCt.Slkh = Me.GridView1.GetDataRow(i).Item("slkh")

            If Not myLoaiChietTinhCtF.Insert(myLoaiChietTinhCt) Then
                XtraMessageBox.Show("Ghi chi tiết vật tư lỗi tại dòng " + i.ToString, "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)

                'xoa tieu de
                Dim myLoaiChietTinhF As New LoaiChietTinhF
                If Not myLoaiChietTinhF.Delete(Me.txtMaLoaiChietTinh.Text.Trim) Then
                    XtraMessageBox.Show("Xóa tiêu đề loại chiết tính không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

                Return False
                Exit Function

            End If
        Next

        Return True
    End Function

    Private Sub AddNew()
        Me_ThemSua = "them"
        Status(1)
        Me.lueLoaiChietTinh.EditValue = ""
        Me.txtMaLoaiChietTinh.EditValue = ""
        Me.txtTenLoaiChietTinh.EditValue = ""
        Me.txtMaLoaiChietTinh.Focus()
        LoadGrid()
    End Sub

    Private Sub cmdThemmoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemLoai.Click
        If Me.cmdThemLoai.Text = "T&hêm loại" Then
            AddNew()

        ElseIf Me.cmdThemLoai.Text = "&Ghi" Then
            If Me_ThemSua = "them" Then
                If Not CheckKey() Then Exit Sub
            End If

            If Not CheckInput() Then Exit Sub

            If Not SaveMaster() Then Exit Sub
            If Not SaveDetail() Then Exit Sub

            XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadLookup()
            Me.lueLoaiChietTinh.EditValue = Me.txtMaLoaiChietTinh.EditValue
            Status(0)

        End If
    End Sub

    Private Sub cmdSuaLoai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSuaLoai.Click
        If Me.cmdSuaLoai.Text = "&Sửa" Then
            Me_ThemSua = "sua"
            Status(2)
            Me.txtMaLoaiChietTinh.Text = Me.lueLoaiChietTinh.EditValue
            Dim myLoaiChietTinhF As New LoaiChietTinhF
            Me.txtTenLoaiChietTinh.Text = myLoaiChietTinhF.GetWhereMaLoaiChietTinh(Me.txtMaLoaiChietTinh.Text.Trim).Rows(0).Item("tenloaichiettinh")
            Me.txtTenLoaiChietTinh.Focus()

        ElseIf Me.cmdSuaLoai.Text = "&Bỏ qua" Then
            Status(0)
            LoadGrid()
        End If
    End Sub

    Private Sub cmdThemVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemVatTu.Click
        AddDetail()
    End Sub

    Private Sub cmdXoaLoai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaLoai.Click
        If XtraMessageBox.Show("Chắc chắn xóa loại chiết tính?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myLoaiCtF As New LoaiChietTinhF
            If myLoaiCtF.Delete(Me.lueLoaiChietTinh.EditValue) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
                LoadLookup()
                frmLoaiChietTinh_MaLoaiChietTinh = ""
                LoadGrid()
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If

    End Sub

    Private Sub cmdThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub cmdXoaVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaVatTu.Click
        Me.GridView1.DeleteSelectedRows()
    End Sub

    Private Sub txtMaLoaiChietTinh_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaLoaiChietTinh.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtTenLoaiChietTinh.Focus()
    End Sub

    Private Sub txtTenLoaiChietTinh_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenLoaiChietTinh.KeyDown
        If e.KeyCode = Keys.Enter Then Me.cmdThemVatTu.Focus()
    End Sub

End Class